export let BezierMaker = function(bezierCtrlNodesArr) {
    this.bezierCtrlNodesArr = bezierCtrlNodesArr ? bezierCtrlNodesArr : []
    this.bezierArr = []
}
BezierMaker.prototype.bezier = function(t) { //贝塞尔公式调用
    var x = 0,
        y = 0,
        bezierCtrlNodesArr = this.bezierCtrlNodesArr,
        n = bezierCtrlNodesArr.length - 1,
        self = this
    bezierCtrlNodesArr.forEach(function(item, index) {
        if(!index) {
            x += item[0] * Math.pow(( 1 - t ), n - index) * Math.pow(t, index) 
            y += item[1] * Math.pow(( 1 - t ), n - index) * Math.pow(t, index) 
        } else {
            x += self.factorial(n) / self.factorial(index) / self.factorial(n - index) * item[0] * Math.pow(( 1 - t ), n - index) * Math.pow(t, index) 
            y += self.factorial(n) / self.factorial(index) / self.factorial(n - index) * item[1] * Math.pow(( 1 - t ), n - index) * Math.pow(t, index) 
        }
    })
    return [x,y]
}
BezierMaker.prototype.getBezierPoints = function() { //通过控制点算出实时xy值渲染到canvas
    var nodeArr = this.bezierCtrlNodesArr  
    if(nodeArr.length === 2) {
        
    } else if(nodeArr.length === 3) {
       
    } else if(nodeArr.length === 4) {
       
    } else {
        var self = this
        for(let i = 0; i < 1; i+=0.01) {
            this.bezierArr.push(this.bezier(i))
        }
        return this.bezierArr;
    }
    
}
BezierMaker.prototype.factorial = function(num) { //递归阶乘
    if (num <= 1) {
        return 1;
    } else {
        return num * this.factorial(num - 1);
    }
}